﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using App.Core.Extensions;
using App.Data;
using App.Core.DB;
using System.Web.Services;

public partial class Pages_Public_ShowEvents : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindEventList(1);
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void BindEventList(int pageNumber)
    {
        UtilityDAO dao = new UtilityDAO();
        int totalRecord = 0;
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_EVENTS, null, pageNumber, ConfigReader.CommonListPageSize, out totalRecord);
        String listItemHtml = @"<div class='ListItem'>                                    
                            <div class='ListTitle'>{0}</div>
                            <div class='ListSubTitle'><b>On:</b> {1}</div>
                            <div style='margin-top:5px;'>
                                {2}
                            </div>
                            <div style='margin-top:5px;'>&gt; <a href='javascript:void(0);' onclick='SendBooking({3});'>Send Booking</a></div>  
                        </div>";
        if (ds.IsEmpty() || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            WebUtil.ShowMessageBox(divMessage, "Sorry! No Event Found.", true);
        else
        {
            StringBuilder html = new StringBuilder(10);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                html.AppendFormat(listItemHtml, NullHandler.GetString(row["Title"]).HtmlEncode()
                    , NullHandler.GetDateTime(row["EventDate"]).ToString(ConfigReader.LongDateDisplayFormat)
                    , NullHandler.GetString(row["Description"]).HtmlEncode()
                    , row["ID"]);
            }
            ltrListing.Text = html.ToString();
            ///Bind the Pager Control
            ucPagerBottom.TotalRecord = totalRecord;
            ucPagerBottom.PageNo = pageNumber;
            ucPagerBottom.PageSize = ConfigReader.CommonListPageSize;
            ucPagerBottom.TotalRecordMessage = "Total {0} Event(s) Found.";
            ucPagerBottom.DataBind();
        }           
    }
    protected void ucPagerBottom_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindEventList(e.PageIndex);
    }

    [WebMethod]
    public static int SaveEventBooking(App.CustomEntity.EventBooking customEventBooking)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        if (context.EventBookings.FirstOrDefault(E => E.EventID == customEventBooking.EventID && String.Compare(E.Email, customEventBooking.Email, true) == 0) == null)
        {
            EventBooking booking = new EventBooking();
            booking.EventID = customEventBooking.EventID;
            booking.Email = customEventBooking.Email;
            booking.Address = customEventBooking.Address;
            booking.ContactNo = customEventBooking.ContactNo;
            booking.Name = customEventBooking.Name;
            booking.Created = DateTime.Now;
            booking.Modified = DateTime.Now;

            context.AddToEventBookings(booking);
            context.SaveChanges();
            return 1;
        }
        else
            return -1;
    }
}